	package be.djdb.generators;
	import java.awt.event.ActionListener;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.JToolBar;

import be.djdb.generators.data.SuperItems;
import be.djdb.utils.SwingConstantsEnum;
	/**
	* @author Lieven Roegiers
	* @copyright 2007
	* @from UpdateCV
	*/
public class Toolbars extends SuperItems{
		private static final boolean DEBUG = false;
		private static final boolean FOLLOW = false;
		private String actiefmenu;
		private Map<String,JToolBar> toolbars = new LinkedHashMap<String,JToolBar>();
		private List<JComponent> tools = new LinkedList<JComponent>();
		private Object jmenu;
		private JToolBar toolbar;
		 /**<%@ page contentType="text/html; charset=" %>
		 * @param String resourcebundlename 
		 * (name => name-lbl.properties)(name => name-tooltip.properties)
		 * or with locale
		 * (name => name-lbl_nl.properties)(name => name-tooltip_nl.properties)
		 * or country with locale
		 * (name => name-lbl_BE_nl.properties)(name => name-tooltip_BE_nl.properties)
		 */
		public Toolbars(String resourcebunlename) {
			super(resourcebunlename);
		}
		public boolean addtoolbaritem(String menu,String lbl,JButton item){
			if (item!=null){
				item.setText(getlblFor(lbl));
				return addmenuitem(menu, lbl,item);
			}else{
				return false;
			}
		}
		public boolean addtoolbaritem(String menu, String lbl, JTextField item ,SwingConstantsEnum  align){
			if (item!=null){
				item.setText(getlblFor(lbl));
				item.setHorizontalAlignment(align.getvalue());
				return addmenuitem(menu, lbl,item);
			}else{
				return false;
			}
		}
		public boolean addtoolbaritem(String menu,String lbl,AbstractButton item){
			if (item!=null){
				item.setText(getlblFor(lbl));
				return addmenuitem(menu, lbl,item);
			}else{
				return false;
			}
		}
		public boolean addtoolbaritem(String menu,String lbl,AbstractButton item,String icon){
			if (item!=null){
				item.setText(getlblFor(lbl));
				addicon(item,icon);
				return addmenuitem(menu, lbl,item);
			}else{
				return false;
			}
		}
		public boolean addtoolbaritem(String menu,String lbl,AbstractButton item,String icon,ActionListener AnAction){
			if (item!=null){
				item.setText(getlblFor(lbl));
				addicon(item,icon);
				item.addActionListener(AnAction);
				return addmenuitem(menu, lbl,item);
			}else{
				return false;
			}
		}
		private void addicon(AbstractButton item,String icon){
			try{
				item.setIcon(new ImageIcon(getClass().getClassLoader().getResource(icon)));
			}catch(NullPointerException e){
				log(Level.CONFIG,"Img:(icon)'"+icon+"'NOT FOUND");
		    }
		}
		private boolean addmenuitem(String menu,String lbl,JComponent item){
			if (DEBUG){	followlog( "additem Amenu To:=> "+ menu +" item:"+ lbl );	}
			if (menu.equals(this.actiefmenu)){
				if (FOLLOW){followlog( menu+" is exist" );	}
				item.setName(lbl);
				item.setToolTipText(getTooltipFor(lbl));
				toolbar.add(item);
				tools.add( item);
			}else{
				if (FOLLOW){followlog( "is not exist" );	}
				createAtoolbars(menu);
				item.setName(lbl);
				item.setToolTipText(getTooltipFor(lbl));
				toolbar.add(item);
				tools.add(item);
				if (DEBUG){followlog("add menu:"+menu+"met label:"+lbl );}
				toolbars.put(menu,toolbar);
			}
			return false;
		}
		private JToolBar createAtoolbars(String name){
	     	if (DEBUG){followlog("createAmenu:=>"+name );}
	     	toolbar = gettoolbar(name);
			if(toolbar != null){
				actiefmenu = name;
				return toolbar;
			}else {
				toolbar = new JToolBar();
				toolbar.setName(name);
				toolbars.put(name, toolbar);
				actiefmenu = name;
				return toolbar;
			}
		}
		public JToolBar gettoolbar(String name){
			return toolbars.get(name);
		}
		public JToolBar getatoolbar(String name){
		       return  (JToolBar)toolbars.get(name);
		}
}

